function bytes(str){
 var len=0;
 for(var i=0;i<str.length;i++){
 	if(str.charCodeAt(i)>127){
 		len++;
 	}
 	len++;
 	}
   return len;
 }

 
String.prototype.replaceAll = function(s1,s2) {
    return this.replace(new RegExp(s1,"igm"),s2);
}

function updatecode() {
	var img = g_site_url+"index.php?user/code/"+Math.random();
	$('#verifycode').attr("src",img);
}

$.dialog.setConfig('base', g_site_url+'css/common/dialog');


function ask_submit(){
	document.searchform.action=g_site_url+'?question/add'+g_suffix;
	document.searchform.submit();
}

function search_submit(){
	word=encodeURI($('#kw').val());
	document.searchform.action=g_site_url+'?question/search/3/'+word+g_suffix;
	document.searchform.submit();
}
function hot_search(index){
	var hot = $("#hot"+index).text();
	$("#kw").val(hot);
	search_submit();
}


function showeditor(id){
	if(!$.xheditor)return false;
	return $('#'+id).xheditor({
		tools:editor_items,
		forcePtag:false,
		upImgUrl:'index.php?attach/upload'
	});
}

Date.prototype.format = function(format){
  var o = {
    "M+" : this.getMonth()+1, //month
    "d+" : this.getDate(),    //day
    "h+" : this.getHours(),   //hour
    "m+" : this.getMinutes(), //minute
    "s+" : this.getSeconds(), //second
    "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
    "S" : this.getMilliseconds() //millisecond
	};
  if(/(y+)/.test(format)) format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
  for(var k in o)if(new RegExp("("+ k +")").test(format))
    format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] :("00"+ o[k]).substr((""+ o[k]).length));
    
  diffseconds=Math.floor((new Date().getMilliseconds()-this.getMilliseconds())/1000 ) ;
  if(diffseconds <= 24*3600) {
	if (diffseconds > 3600) {
		format = intval(diffseconds/3600)+'小时前';
	} else if (diffseconds > 60) {
		format = intval(diffseconds/60)+'分钟前';
	} else if (diffseconds > 0) {
		format = diffseconds+'秒前';
	} else {
		format = '现在前';
	}
  }
  return format;
}

Date.prototype.timeLength = function(time){
    length='';
    if(day = floor(time/(24*3600))){
        length += day+'天';
    }
    if(hour = floor(time % (24*3600)/3600)){
        length += hour+'小时';
    }
    if(day==0 && hour==0){
        length = floor(time/60)+'分';
    }
    return length;
}

var copy2Clipboard=function(txt){
	if(window.clipboardData){
		window.clipboardData.clearData();
		window.clipboardData.setData("Text",txt);
	}
	else if(navigator.userAgent.indexOf("Opera")!=-1){
		window.location=txt;
	}
	else if(window.netscape){
		try{
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
		}
		catch(e){
			alert("您的firefox安全限制限制您进行剪贴板操作，请打开’about:config’将signed.applets.codebase_principal_support’设置为true’之后重试，相对路径为firefox根目录/greprefs/all.js");
			return false;
		}
		var clip=Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
		if(!clip)return;
		var trans=Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
		if(!trans)return;
		trans.addDataFlavor('text/unicode');
		var str=new Object();
		var len=new Object();
		var str=Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
		var copytext=txt;str.data=copytext;
		trans.setTransferData("text/unicode",str,copytext.length*2);
		var clipid=Components.interfaces.nsIClipboard;
		if(!clip)return false;
		clip.setData(trans,null,clipid.kGlobalClipboard);
	}
}

function share(){
	title=$.trim($('#question_title').text())+"\n"; 
	$('#left2').prepend(
	"<strong>分享到:</strong><a href='javascript:void(0);' onclick=\"javascript:window.open(\'http:\/\/v.t.sina.com.cn\/share\/share.php?title=\'+encodeURIComponent(document.title.substring(0,76))+\'&url=\'+encodeURIComponent(location.href)+\'&rcontent=\',\'_blank\',\'scrollbars=no,width=600,height=450,left=75,top=20,status=no,resizable=yes\'); void 0\" style=\"color:#000000;text-decoration:none;font-size:12px;font-weight:normal\"><SPAN style=\"PADDING: 10px;  FONT-SIZE: 12px; PADDING-BOTTOM: 0px; MARGIN-LEFT: 1px; CURSOR: pointer; PADDING-TOP: 0px\"><IMG alt=新浪微博 src=\"http:\/\/t.sina.com.cn\/favicon.ico\" width=20 height=16 align=absMiddle border=0>&nbsp;新浪微博<\/SPAN><\/a>"
			+"<a href='javascript:void(0);' onclick=\"javascript:window.open(\'http:\/\/www.kaixin001.com\/repaste\/share.php?rtitle=\'+encodeURIComponent(document.title.substring(0,76))+\'&rurl=\'+encodeURIComponent(location.href)+\'&rcontent=\',\'_blank\',\'scrollbars=no,width=600,height=450,left=75,top=20,status=no,resizable=yes\'); void 0\" style=\"color:#000000;text-decoration:none;font-size:12px;font-weight:normal\"><SPAN style=\"PADDING: 10px;  FONT-SIZE: 12px; PADDING-BOTTOM: 0px; MARGIN-LEFT: 1px; CURSOR: pointer; PADDING-TOP: 0px\"><IMG alt=开心网 src=\"http:\/\/img1.kaixin001.com.cn\/i\/favicon.ico\" align=absMiddle border=0>&nbsp;开心网<\/SPAN><\/a>"
			+"<a href='javascript:void(0);' onclick=\"javascript:window.open(\'http://share.renren.com/share/buttonshare.do?title=\'+encodeURIComponent(document.title.substring(0,76))+\'&link=\'+encodeURIComponent(location.href)+\'&content=\',\'_blank\',\'scrollbars=no,width=600,height=450,left=75,top=20,status=no,resizable=yes\'); void 0\" style=\"color:#000000;text-decoration:none;font-size:12px;font-weight:normal\"><SPAN style=\"PADDING: 10px; FONT-SIZE: 12px; PADDING-BOTTOM: 0px; MARGIN-LEFT: 1px; CURSOR: pointer; PADDING-TOP: 0px\"><IMG alt=人人网 src=\"http:\/\/s.xnimg.cn\/favicon-rr.ico\" align=absMiddle border=0>&nbsp;人人网<\/SPAN><\/a>"
			+"<a href='javascript:void(0);' onclick=\"javascript:window.open(\'http://v.t.qq.com/share/share.php?title=\'+encodeURIComponent(document.title.substring(0,76))+\'&url=\'+encodeURIComponent(location.href),\'_blank\',\'scrollbars=no,width=600,height=450,left=75,top=20,status=no,resizable=yes\'); void 0\" style=\"color:#000000;text-decoration:none;font-size:12px;font-weight:normal\"><SPAN style=\"PADDING: 10px; FONT-SIZE: 12px; PADDING-BOTTOM: 0px; MARGIN-LEFT: 1px; CURSOR: pointer; PADDING-TOP: 0px\"><IMG alt=腾讯微博 src=\"http:\/\/gb.cri.cn\/news\/include\/qqwb.png\" align=absMiddle border=0>&nbsp;腾讯微博<\/SPAN><\/a>"
				+"<a href=\"javascript:copy2Clipboard(title+location.href);alert('已经复制到粘贴板，你可以使用Ctrl+V 贴到需要的地方去了哦！  ');void 0\" style=\"color:#000000;text-decoration:none;\"><span style=\"padding-right: 2px; padding-left: 20px; font-size: 12px;color:blue; padding-bottom: 0px; margin-left: 1px; cursor: pointer; padding-top: 0px\">&nbsp;[通过复制分享给QQ、msn好友]<\/span><\/a>"
		
	);
}

function getcookie(name) {
	var cookie_start = document.cookie.indexOf(name);
	var cookie_end = document.cookie.indexOf(";", cookie_start);
	return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
}

function setcookie(cookieName, cookieValue, seconds, path, domain, secure) {
	seconds = seconds ? seconds : 8400000;
	var expires = new Date();
	expires.setTime(expires.getTime() + seconds);
	document.cookie = escape(cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '/')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');
}


function addfavorite(){
   if (document.all){
      window.external.addFavorite(g_site_url,document.title);
   }else if (window.sidebar){
      window.sidebar.addPanel(document.title,g_site_url, "");
   }
}

function SetHomepage(){
   if(document.all){
      document.body.style.behavior="url(#default#homepage)";
      document.body.setHomePage(g_site_url);
   }
   else if(window.sidebar){
        if(window.netscape){
             try{
                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
             }
             catch(e){
                 alert("您的浏览器未启用[设为首页]功能，开启方法：先在地址栏内输入about:config,然后将项 signed.applets.codebase_principal_support 值该为true即可");
             }
        }
        var prefs=Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
        prefs.setCharPref("browser.startup.homepage",g_site_url);
   }
}

